<?php 
session_start();
class product extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','','smarty');
            $this->_configs["title"]="Quản Lý Deal";
            $this->_configs["authority"]="deal";
            $this->_configs["MinAuthority"]=0;
            $this->load->model('deal/category_model','category_model');
            $this->load->model('deal/product_model','product_model');
            $this->load->model('deal/customer_model','customer_model');
            $this->load->model('admin-planners/region_model','region_model');
            $_SESSION["DRegion"]=isset($_SESSION["DRegion"])?$_SESSION["DRegion"]:"";
            $_SESSION["DRegionName"]=isset($_SESSION["DRegionName"])?$_SESSION["DRegionName"]:"Toàn Quốc";
        }
        public function index()
	{
            $Configs=null;
            $Configs["menu"]="deal";
            $Configs["U"]["avata"]="default";
            $Configs["U"]["email"]="";
            if(isset($_SESSION["D-USER"])){$Configs["U"]=$_SESSION["D-USER"];}
            $num=8;
            $begin=0;
            $data["categorys"]  =   objectToArray($this->category_model->gets());
            $data["products"]   =   objectToArray($this->product_model->gets($begin,$num));
            for ($i=0;$i<count($data["products"]);$i++){
                $data["products"][$i]["Discount"]=round(($data["products"][$i]["UnitPrice"]-$data["products"][$i]["UnitsOnOrder"])*100/$data["products"][$i]["UnitPrice"],0);
            }
            $Configs["Foot"]=@file_get_contents(APPPATH."HTML_DOM/Foot.txt");
            $data["regions"]=  objectToArray($this->region_model->getByParent("0"));
            
            $this->smarty->assign('data', $data);
            $this->smarty->assign('Configs', $Configs);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->view     (   "deal/subs/00_css"       ,"dcss"         );
            $this->smarty->view     (   "deal/subs/00_script"    ,"dscript"      );
            $this->smarty->view     (   "deal/subs/01_head"      ,"dhead"        );
            $this->smarty->view     (   "deal/subs/02_menu"      ,"dmenu"        );
            $this->smarty->view     (   "deal/subs/03_foot"      ,"dfoot"        );
            $this->smarty->view     (   "deal/pages/01_home"     ,"dpage"        ); 
            $this->smarty->display  (   "deal/01_home"                          );
	}
        public function checkproduct($alias=""){
            if(!isset($_SESSION["D-USER"]) && false){
                $code=-1;
                $msg="Bạn chưa đăng nhập.";
            }else{
                $data["product"]   =   objectToArray($this->product_model->getByAlias($alias));
                if(count($data["product"])==0){
                    $code=-2;
                    $msg="Không tìm thấy sản phẩm này.";
                }else{
                    $product=$data["product"][0];
                    if(
                            isset($product["Delete"]) 
                            || $product["Discontinued"]=="1" 
                            || (strtotime("now")-strtotime($product["EndDate"]))>0
                            || $product["Amount"]<=$product["Sold"]
                            || $product["Status"]=="Private"
                            || $product["Status"]=="false"
                            ){
                        $code=-3;
                        $msg="Sản phẩm đang ngừng bán hoặc đã hết hàng.";
                    }else{
                        $code=1;
                        $msg="Thành Công.";
                    }
                }
            }
            $rs=array("code"=>$code,"msg"=>$msg);
            echo json_encode($rs);
        }
        public function detail($alias="")
	{
            $Configs=null;
            $Configs["menu"]="dealdetail";
            $Configs["U"]["avata"]="default";
            $Configs["U"]["email"]="";
            
            $Configs["alias"]=$alias;
            if(isset($_SESSION["D-USER"])){$Configs["U"]=$_SESSION["D-USER"];}
            $num=8;
            $begin=0;
            $data["categorys"]  =   objectToArray($this->category_model->gets());
            $data["product"]   =   objectToArray($this->product_model->getByAlias($alias));
            if(count($data["product"])==0){
                 show_404();
                 return;
            }
            $data["product"]=$data["product"][0];
            $Configs["tag"]=$data["product"]["Tag"];
            $data["product"]["Album"]=  json_decode($data["product"]["Album"]);
            
            $data["otherproduct"]=objectToArray($this->product_model->getOtherByTag($data["product"]["ProductID"],$data["product"]["Tag"],$data["product"]["Categorys"]));
            
            $data["product"]["Discount"]=round(($data["product"]["UnitPrice"]-$data["product"]["UnitsOnOrder"])*100/$data["product"]["UnitPrice"],0);
            
            $Configs["Foot"]=@file_get_contents(APPPATH."HTML_DOM/Foot.txt");
            $data["regions"]=  objectToArray($this->region_model->getByParent("0"));
            
            $this->smarty->assign('data', $data);
            $this->smarty->assign('Configs', $Configs);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->view     (   "deal/subs/00_css"       ,"dcss"         );
            $this->smarty->view     (   "deal/subs/00_script"    ,"dscript"      );
            $this->smarty->view     (   "deal/subs/01_head"      ,"dhead"        );
            $this->smarty->view     (   "deal/subs/02_menu"      ,"dmenu"        );
            $this->smarty->view     (   "deal/subs/03_foot"      ,"dfoot"        );
            $this->smarty->view     (   "deal/pages/10_detail"     ,"dpage"        ); 
            $this->smarty->display  (   "deal/01_home"                          );
	}
        
          
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */